﻿#include "LogicDesignerPlugin.h"
#include "LogicDesignerNodeFactory.h"
#include <QDebug>
#include <QTimer>
// interface
#include "DACoreInterface.h"
#include "DADockingAreaInterface.h"
#include "DAActionsInterface.h"
#include "DAProjectInterface.h"
#include "DAUIInterface.h"
// widgets
#include "DAChartManageWidget.h"
#include "DAChartOperateWidget.h"
#include "DADataManageWidget.h"
#include "DADataOperateWidget.h"
#include "DARibbonAreaInterface.h"
#include "DATranslatorManeger.h"
#include "DAWorkFlowEditWidget.h"
#include "DAWorkFlowOperateWidget.h"
#include "DASettingContainerWidget.h"
#include "DAWorkFlowNodeItemSettingWidget.h"
// SARibbon
#include "SARibbonBar.h"
#include "SARibbonCategory.h"
#include "SARibbonPannel.h"
#include "SARibbonQuickAccessBar.h"
#include "SARibbonMainWindow.h"
LogicDesignerPlugin::LogicDesignerPlugin() : DA::DAAbstractNodePlugin()
{
}

LogicDesignerPlugin::~LogicDesignerPlugin()
{
}

bool LogicDesignerPlugin::initialize()
{
    // 通过这里可以隐藏掉不要的dock
    DA::DADockingAreaInterface* dockArea    = core()->getUiInterface()->getDockingArea();
    DA::DAActionsInterface* actionInterface = core()->getUiInterface()->getActionInterface();
    dockArea->hideDockWidget(dockArea->getChartOperateWidget());
    dockArea->hideDockWidget(dockArea->getChartManageWidget());
    dockArea->hideDockWidget(dockArea->getDataManageWidget());
    dockArea->hideDockWidget(dockArea->getDataOperateWidget());
    // 设置窗口把不需要的窗口先隐藏
    if (DA::DASettingContainerWidget* settingWidget = dockArea->getSettingContainerWidget()) {
        if (DA::DAWorkFlowNodeItemSettingWidget* nodeSettingWidget = settingWidget->getWorkFlowNodeItemSettingWidget()) {
            // nodeSettingWidget->removeItemSettingTab();
            // nodeSettingWidget->removeLinkSettingTab();
            // nodeSettingWidget->removeNodeSettingTab();
            nodeSettingWidget->removePictureItemSettingTab();
        }
    }
    // 通过这里隐藏掉不需要的ribbon
    DA::DARibbonAreaInterface* ribbonArea = core()->getUiInterface()->getRibbonArea();
    SARibbonCategory* c = ribbonArea->getCategoryByObjectName(QStringLiteral("da-ribbon-category-data"));
    if (c) {
        ribbonArea->ribbonBar()->hideCategory(c);
    }
    c = ribbonArea->getCategoryByObjectName(QStringLiteral("da-ribbon-category-edit"));
    if (c) {
        ribbonArea->ribbonBar()->hideCategory(c);
    }
    c = ribbonArea->getCategoryByObjectName(QStringLiteral("da-ribbon-category-figure"));
    if (c) {
        ribbonArea->ribbonBar()->hideCategory(c);
    }
    // 把main的部分pannel隐藏
    SARibbonPannel* pannel = ribbonArea->getPannelByObjectName("da-pannel-main.data-opt");
    if (pannel) {
        pannel->hide();
    }
    pannel = ribbonArea->getPannelByObjectName("da-pannel-main.chart-opt");
    if (pannel) {
        pannel->hide();
    }

    //    pannel = ribbonArea->getPannelByObjectName("da-pannel-main.workflow");
    //    if (pannel) {
    //        pannel->hide();
    //    }
    // 把工作流编辑改名为“建模”
    SARibbonCategory* category = ribbonArea->getCategoryByObjectName("da-ribbon-category-workflow.edit");
    if (category) {
        // 改名字会被程序加载完的语言替换给替换回原来的
        //  category->setCategoryName(u8"建模");设置后后面会有个LanguageChang事件，又把文字替换回去了
        // 因此用个定时器替换
        QTimer::singleShot(500, this, [ category ]() { category->setCategoryName(u8"建模"); });
    }

	loadSetting();
	return DA::DAAbstractNodePlugin::initialize();
}

QString LogicDesignerPlugin::getIID() const
{
	return "Plugin.LogicDesignerPlugin";
}

QString LogicDesignerPlugin::getName() const
{
	return u8"Logic Designer Plugin";
}

QString LogicDesignerPlugin::getVersion() const
{
	return "0.1.0";
}

QString LogicDesignerPlugin::getDescription() const
{
	return u8"Code free configuration logic designer for stress testing equipment";
}

DA::DAAbstractNodeFactory* LogicDesignerPlugin::createNodeFactory()
{
    auto fac = new LogicDesignerNodeFactory();
	fac->setCore(core());
	connect(fac, &LogicDesignerNodeFactory::destroyed, this, &LogicDesignerPlugin::onFactoryDestroyed);
	return fac;
}

void LogicDesignerPlugin::destoryNodeFactory(DA::DAAbstractNodeFactory* p)
{
	if (p) {
		p->deleteLater();
	}
}

DA::DAAbstractSettingPage* LogicDesignerPlugin::createSettingPage()
{
	//! 对于插件的设置页，如果需要设置，这里要返回设置页面
	//! 返回nullptr代表没有设置页面
	return nullptr;
}

void LogicDesignerPlugin::onFactoryDestroyed(QObject* obj)
{
	//! 每个插件会有多个工厂，每个工作流会产生一个工厂，工厂的删除会触发此槽函数，如果不需要，可以删除
	//! 槽函数的链接在createNodeFactory中进行
	qDebug() << "one factory have removred";
}

bool LogicDesignerPlugin::loadSetting()
{
	//! 这里添加加载配置文件的内容，插件的配置信息从这里加载，此函数会在initialize中调用
	return true;
}
